package com.example.weatherforecast.citypagetool;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author：coderChen
 * @Package：com.example.weatherforecast.function
 * @Project：WeatherForecast
 * @Date：2023/12/31 15:50
 * @Filename：SwipeToDeleteCallback
 * Explain:城市管理->滑动删除指定城市
 */
public class SwipeToDelete extends ItemTouchHelper.SimpleCallback {

    private ManageActivity_ButtonAdapter adapter;
    private SharedPreferences sharedPreferences;

    public SwipeToDelete(ManageActivity_ButtonAdapter adapter, SharedPreferences sharedPreferences) {
        super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
        this.adapter = adapter;
        this.sharedPreferences = sharedPreferences;
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        int position = viewHolder.getAdapterPosition();

        // 创建一个 AlertDialog.Builder
        AlertDialog.Builder builder = new AlertDialog.Builder(viewHolder.itemView.getContext());
        builder.setMessage("确定要删除这个城市吗？")
                .setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        adapter.removeCity(position, sharedPreferences);
                        dialog.dismiss();
                        // 显示删除成功的提示
                        Toast.makeText(viewHolder.itemView.getContext(), "城市删除成功", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        adapter.notifyItemChanged(position); // 取消时恢复原状
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}





